package calendarAlarmService

import (
	"GoLangTest/config"
	"GoLangTest/model/calendarAlarm"
	"GoLangTest/util/websocketUtil"
	"fmt"
	"github.com/gorilla/websocket"

	"time"
)

func Add(information calendarAlarm.CalendarAlarmInformation) (bool, error) {
	tx := config.GetDB().Begin()
	err := tx.Create(&information).Error
	if err != nil {
		tx.Rollback()
	}
	tx.Commit()
	return true, err
}

func GetListByCreateUserId(createUserId int) (list []calendarAlarm.CalendarAlarmInformation) {
	config.GetDB().Where("create_user_id = ?", createUserId).Find(&list)
	return list
}

func Update(information calendarAlarm.CalendarAlarmInformation) (bool, error) {
	tx := config.GetDB().Begin()
	err := tx.Model(&calendarAlarm.CalendarAlarmInformation{}).Where("id = ? AND create_user_id = ?", information.Id, information.CreateUserId).Updates(&information).Error
	if err != nil {
		tx.Rollback()
	}
	tx.Commit()
	return true, err
}

func DeleteById(information calendarAlarm.CalendarAlarmInformation) (bool, error) {
	tx := config.GetDB().Begin()
	err := tx.Where("id = ? AND create_user_id = ? ", information.Id, information.CreateUserId).Delete(&calendarAlarm.CalendarAlarmInformation{}).Error
	if err != nil {
		tx.Rollback()
	}
	tx.Commit()
	return true, err
}

func getListByAlarmCount(alarmCount int) (list []calendarAlarm.CalendarAlarmInformation) {
	nowTime := time.Now()
	nowTimeStr := nowTime.Format("2006-01-02 15:04:05")
	config.GetDB().Where("alarm_count < ? AND alarm_time <= ?", alarmCount, nowTimeStr).Find(&list)
	return list
}

func getListByAlarmCountAndCreateUserId(alarmCount int, createUserId int) (list []calendarAlarm.CalendarAlarmInformation) {
	nowTime := time.Now()
	nowTimeStr := nowTime.Format("2006-01-02 15:04:05")
	config.GetDB().Where("alarm_count < ? AND create_user_id = ? AND alarm_time <= ?", alarmCount, createUserId, nowTimeStr).Find(&list)
	return list
}

/* 提醒用户 */
func AlarmUser() {
	//获取超过提醒时间且提醒次数小于1的数据
	list := getListByAlarmCount(1)

	//遍历结果并进行提醒
	for _, information := range list {
		//判断类型采用对应的提醒方式
		switch information.AlarmType {
		case "EMAIL":
			emailAlarm(information)
			break
		case "SMS":
			smsAlarm(information)
			break
		}
	}
}

/* 提醒当前用户 */
func AlarmUserByCreateUserId(createUserId int, ws *websocket.Conn) {
	//获取超过提醒时间且提醒次数小于1的数据
	list := getListByAlarmCountAndCreateUserId(1, createUserId)
	//遍历结果并进行提醒
	for _, information := range list {
		messageStr := fmt.Sprintf("您有一条日历提醒内容-%d：%s", information.Id, information.Content)
		fmt.Println("websocket提醒：", messageStr)
		websocketUtil.SendMessagesToClient(ws, messageStr)
	}
}

func emailAlarm(information calendarAlarm.CalendarAlarmInformation) {
	//TODO email提醒方式
	fmt.Println("email提醒")
	//一般来讲建个163的邮箱然后通过http请求对应的Api进行发送

	//提醒成功把提醒次数加一
	information.AlarmCount = information.AlarmCount + 1
	config.GetDB().Model(&calendarAlarm.CalendarAlarmInformation{}).Where("id = ?", information.Id).Updates(calendarAlarm.CalendarAlarmInformation{AlarmCount: information.AlarmCount})
}

func smsAlarm(information calendarAlarm.CalendarAlarmInformation) {
	//TODO 短信提醒方式
	fmt.Println("短信提醒")
	//购买对应的短信服务，并使用对应API进行短信发送

	//提醒成功把提醒次数加一
	information.AlarmCount = information.AlarmCount + 1
	config.GetDB().Model(&calendarAlarm.CalendarAlarmInformation{}).Where("id = ?", information.Id).Updates(calendarAlarm.CalendarAlarmInformation{AlarmCount: information.AlarmCount})
}
